<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.Search"
        clearable
       placeholder="负责人姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"
/>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        点击搜索
      </el-button>

      <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增负责人</el-button>
    </div>

    <el-table v-loading="listLoading" :data="listData.Items" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.ID }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="负责人姓名">
        <template slot-scope="scope">
          {{ scope.row.Name }}
        </template>
      </el-table-column>

      <el-table-column align="header-center" label="备注">
        <template slot-scope="scope">
          {{ scope.row.Note }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="handleEdit(scope)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listData.Total > 0"
      :total="listData.Total"
      :page.sync="listQuery.Page"
      :limit.sync="listQuery.PageSize"
      @pagination="getListData"
    />

    <el-dialog :visible.sync="dialogVisible" :title="dialogType === 'edit' ? '编辑负责人' : '新增负责人'">
      <el-form :model="oneData" label-width="80px" label-position="left">
        <el-form-item label="姓名">
          <el-input
            v-model="oneData.Name"
            placeholder="负责人姓名"
          />
        </el-form-item>

        <el-form-item label="备注">
          <el-input
            v-model="oneData.Note"
            :autosize="{ minRows: 1, maxRows: 3 }"
            type="textarea"
            placeholder="备注"
          />
        </el-form-item>

      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmEdit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import * as data_api from '@/api/warehouse/person'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const PersonType = 1// 商场负责人

const defaultData = {
  Name: '', // 促销员名称
  Note: '', // 备注
  Type: PersonType,
}

export default {
  components: { Pagination },
  data() {
    return {
      listData: {
        Items: [],
        Total: 0
      },
      listLoading: false,
      listQuery: {
        Page: 1,
        PageSize: 10,
        Search: '',
        Sorts: 'name ASC',
        Type: PersonType,
      },
      oneData: Object.assign({}, defaultData),
      dialogVisible: false,
      dialogType: 'new',
    }
  },
  computed: {
  },
  watch: {
  },
  mounted() {
  },
  created() {
    this.getListData()
  },
  methods: {
    getListData() {
      this.listLoading = true
      data_api.list(this.listQuery).then(res => {
        this.listData = res.data
        this.normalizeData()
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },

    normalizeData() {
    },

    handleFilter() {
      this.listQuery.Page = 1
      this.getListData()
    },

    async confirmEdit() {
      const isEdit = this.dialogType === 'edit'

      if (!isEdit) {
        await data_api.create(this.oneData)
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: `新增超市促销员: ${this.oneData.Name} 成功`
        })
      } else {
        await data_api.update(this.oneData)
        this.dialogVisible = false
        this.$message({
          type: 'success',
          message: `更新超市促销员: ${this.oneData.Name} 成功`
        })
      }
      this.getListData()
    },
    handleAdd() {
      this.oneData = Object.assign({}, defaultData)
      this.oneData.name
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.oneData = deepClone(scope.row)
    },
  }
}
</script>

  <style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }
  }
</style>
